package com.kitty.game.fight.artifact.passive;

import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.artifact.ArtifactService;
import com.kitty.game.fight.bean.PlayerFightObject;
import com.kitty.game.fight.bean.SuperBossFightObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class GourdArtifactEffect extends ArtifactEffect {

    @Override
    public short getEffectId() {
        return 8398;
    }

    @Override
    public ArtifactType getType() {
        return ArtifactType.GOURD;
    }

    @Override
    public Boolean trigger(Fight fight, FightObject member, Object... params) {
//        log.error("卸甲金葫=={}=={}",member.getName(),member.getType());
        if (!checkBaseCondition1(fight, member)) {
            return false;
        }
        int firstTarget = member.getTargetId();
        FightObject target = fight.queryFightObject(firstTarget);
        // 目标不是玩家，对方肯定没有法宝 如果目标是队友也不触发
        if (target == null || target.isASide() == member.isASide()) {
            return false;
        }
        ArtifactService artifactService = SpringUtils.getBean(ArtifactService.class);
        if (member.getRole() == null){
            SuperBossFightObject playerObj = (SuperBossFightObject) member;
            Map<String, Integer> artifactMap = playerObj.getArtifactMap();
            boolean boo = artifactService.getArtifactRate(artifactMap.get(getType().getName()));
//            logger.error("触发卸甲金葫=={}=={}=={}",member.getName(),target.getName(),boo);
            return boo;
        }
        return artifactService.getArtifactRate(getType().getName(), member.getRole());
    }

    protected boolean checkBaseCondition1(Fight fight, FightObject actor) {
//        if (!actor.isPlayer()) {
//            return false;
//        }
        // 当前回合我的法宝被别人的卸甲金葫禁锢了
        if (fight.isMyArtifactInvalid(actor.getId())) {
            return false;
        }
        if (actor instanceof PlayerFightObject){
            PlayerFightObject playerObj = (PlayerFightObject) actor;
            ArtifactService artifactService = SpringUtils.getBean(ArtifactService.class);
            return getType().getName().equals(artifactService.getEquippedArtifact(playerObj.getRole()));
        }else if (actor instanceof  SuperBossFightObject){
            SuperBossFightObject playerObj = (SuperBossFightObject) actor;
            Map<String, Integer> artifactMap = playerObj.getArtifactMap();
            boolean boo = artifactMap.get(getType().getName()) != null;
//            logger.error("世界boss触发卸甲金葫=={}={}={}", Json.toJson(artifactMap, JsonFormat.compact()),getType().getName(),boo);
            return boo;
        }else {
            return false;
        }

    }

}
